package com.wifi.eshipewifi;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wifi.BaseApplication;
import com.wifi.imp.MessageListener;
import com.wifi.imp.MessageSend;

public class PassWordSettingActivity extends AppCompatActivity implements View.OnClickListener,MessageListener {
    ImageButton back;
    EditText et_wifi_ssid;
    EditText et_wifi_pass;
    LinearLayout ll_wifi;
    RelativeLayout rl_wifi_sure;
    TextView tittle;
    BaseApplication application;
    String TAG="WifiSettingActivity";
    SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password_setting);
        application = (BaseApplication) getApplication();

        et_wifi_ssid = (EditText) findViewById(R.id.et_wifi_ssid);
        et_wifi_pass = (EditText) findViewById(R.id.et_wifi_pass);
        ll_wifi = (LinearLayout) findViewById(R.id.ll_wifi);
        rl_wifi_sure = (RelativeLayout) findViewById(R.id.rl_wifi_sure);
        back = (ImageButton) findViewById(R.id.ib_back);
        tittle = (TextView) findViewById(R.id.tv_include_tittle);
        tittle.setText("账号设置");
        back.setOnClickListener(this);
        rl_wifi_sure.setOnClickListener(this);
        ll_wifi.getBackground().setAlpha(100);

        sp= getSharedPreferences("wifiSetting", Activity.MODE_PRIVATE);
        String ssid = sp.getString("login_name", "SN000000");//todo 以后要改
        String password = sp.getString("login_pass", "12345678");
        et_wifi_ssid.setText(ssid);
        et_wifi_pass.setText(password);
    }

    @Override
    protected void onResume() {//每个activity都要加这一句
        super.onResume();
        if(application==null)
            application = (BaseApplication) getApplication();
        application.setMsgListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ib_back:
                this.finish();
                break;

            case R.id.rl_wifi_sure:
                String ssid=et_wifi_ssid.getText().toString().trim();
                String password=et_wifi_pass.getText().toString().trim();
                if(ssid!=null && password!=null){
                    if(password.length()<8){
                        application.makeToast(PassWordSettingActivity.this,"密码不得低于8位",true);
                    }else {
                        application.makeToast(PassWordSettingActivity.this,"上传成功 ssid:"+ssid+"密码："+password,true);
                        //// TODO: 2016/8/13 命令沟通
                        application.sendMsg("$reset;"+ssid+","+password+";");
                    }
                }
                break;
        }
    }

    @Override
    public void MessageGetListener(String msg) {
        try {
            if (msg != null) {
                String s = msg;
                if (s.trim().length() > 0) {
                    Log.i(TAG, "mhandler接收到obj=" + s);
                    String[] s1=s.split(";");
                    if(s1[0].indexOf("$reset")!=-1){
                        //todo 断线重连 后台账号密码重连
                        String name = et_wifi_ssid.getText().toString().trim();
                        String password = et_wifi_pass.getText().toString().trim();
                        //保存修改后的ssid
                        SharedPreferences.Editor edit = sp.edit();
                        edit.putString("login_name",name);
                        edit.putString("login_pass",password);
                        //提交当前数据
                        edit.commit();
                        //使用toast信息提示框提示成功写入数据
                        application.makeToast(PassWordSettingActivity.this,"修改成功",true);
                    }
                }
            }
            Log.i(TAG, "mhandler接收到msg" + msg);
        } catch (Exception ee) {
            Log.i(TAG, "加载过程出现异常");
            ee.printStackTrace();
        }
    }
}
